/*
    Create Date : 
    Author    : Nguyen Duy Tan
    Copyright(c) 2009 A.D.A Solution. All rights reserved
    =====================================================
    Update History:
    Ver.    TRB#            Date                Author        Note
    1        V001           2010/01/12          Toannn        Review lần 2 bị lổi
    1        V002           2010/02/04          Toannn        Thêm chức năng cập nhật lại Phương tiện khi Tracking    
    2.0     TRB003          2010/10/13          Tannd         Fix bug Zoom 2 level lớn nhất ở google lỗi     
*/
//alert(sLang);
var muststop 	= GOOGLEFUNCS_MUST_STOP;
var showlist 	= GOOGLEFUNCS_SHOW_LIST;
var hidelist 	= GOOGLEFUNCS_HIDE_LIST;
var sorryg 		= GOOGLEFUNCS_SORRY_G;
var alldevice 	= GOOGLEFUNCS_ALL_DEVICE;
var f30t999 	= GOOGLEFUNCS_30_999;
var connect 	= GOOGLEFUNCS_CONNECT;
var nodata 		= GOOGLEFUNCS_NO_DATA;
var dataerr 	= GOOGLEFUNCS_DATA_ERR;
var stopfor 	= GOOGLEFUNCS_STOPED;
var lostfor 	= GOOGLEFUNCS_LOST_FOR;
var gpsfor 		= GOOGLEFUNCS_LOST_GPS;
var unknown 	= GOOGLEFUNCS_UNKNOW;
var devicename 	= GOOGLEFUNCS_DEVICENAME;
var time 		= GOOGLEFUNCS_TIME;
var ttime 		= GOOGLEFUNCS_TTIME;
var speed 		= GOOGLEFUNCS_STATUS;
var distance 	= GOOGLEFUNCS_DISTANCE;
var distance_review     = GOOGLEFUNCS_DISTANCE_REVIEW;
var tdistance 	= GOOGLEFUNCS_DISTANCE_TOTAL;
var strActive	= GOOGLEFUNCS_ACTIVE;
var satellite 	= GOOGLEFUNCS_SATELLITE;
var moving 		= GOOGLEFUNCS_MOVING;
var splay 		= GOOGLEFUNCS_PLAY;
var sstop		= GOOGLEFUNCS_STOP;
var driver		= GOOGLEFUNCS_DRIVER;
var l			= GOOGLEFUNCS_LOCATION;
var latlong		= GOOGLEFUNCS_LAT_LONG

var new_loc		= GOOGLEFUNCS_ADD_NEW;
var new_name	= GOOGLEFUNCS_NEW_NAME;
var new_loc_type = GOOGLEFUNCS_NEW_LOC_TYPE;
var new_loc_type_select = GOOGLEFUNCS_SELECT_TYPE;
var new_address = GOOGLEFUNCS_ADDRESS;
var new_contact = GOOGLEFUNCS_CONTACT;
var new_phone = GOOGLEFUNCS_PHONE;
var new_amount = GOOGLEFUNCS_AMOUNT;
var new_images = GOOGLEFUNCS_IMAGE;
var new_radius	= GOOGLEFUNCS_RADIUS;
var units	= GOOGLEFUNCS_UNITS;	
var description	= GOOGLEFUNCS_REMARK;
var btnsave		= GOOGLEFUNCS_SAVE;
var btncancel		= GOOGLEFUNCS_CANCEL;

var map1 = GOOGLEFUNCS_MAP1;
var map2 = GOOGLEFUNCS_MAP2;
var map3 = GOOGLEFUNCS_MAP3;
var map4 = GOOGLEFUNCS_MAP4;
var map5 = GOOGLEFUNCS_MAP5;
//var vietBD = GOOGLEFUNCS_VIEPMAP;
var power_str = GOOGLEFUNCS_POWER;    // ĐậmPV: 2010-10-11 - Thêm trạng thái nguồn
var strinput1 	= GOOGLEFUNCS_INPUT1;
var strinput2 	= GOOGLEFUNCS_INPUT2;
var strinput3On 	= GOOGLEFUNCS_INPUT3_ON;
var strinput3Off 	= GOOGLEFUNCS_INPUT3_OFF;
var strinput4On 	= GOOGLEFUNCS_INPUT4_ON;
var strinput4Off 	= GOOGLEFUNCS_INPUT4_OFF;
var strinput_5On 	= GOOGLEFUNCS_INPUT5_ON;
var strinput_5Off 	= GOOGLEFUNCS_INPUT5_OFF;
//=================================================================================================
//start Trung add
var Gmap ;
var Gwaypoints ;
var Gmarker ;
var Gobjpolyline  ;
var drawMarker = null;
var latinit = 10.7654865; 
var lnginit = 106.67793273;
// end trung add
function GoogleFuncs()
{
	var obj=new Object();
	obj.latitudeCurrent=10;
	obj.longitudeCurrent;
	obj.levelCurrent;
	obj.map;
	obj.global=new Object();
	obj.arrIcon = new Array;
   // var map;
    
	var baseIcon;
	var baseIcon2;
    var baseIcon4; 
	//var icn;
	obj.locCreating;
	obj.labele;
    obj.wikiMoveEnd = null;
    obj.wikiZoomEnd = null;
    obj.currentMapName="";    

	obj.InitMap =function(div, type)
	{
		type = type || 0;		// ĐậmPV: 2010-10-27 - Add: 0: Gmaps, 1: vngeobiz
	    if (GBrowserIsCompatible()) 
		{
			obj.map = new GMap2(document.getElementById(div));
			obj.map.addControl(new GScaleControl());
			obj.map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 25)));
			baseIcon = new GIcon(G_DEFAULT_ICON);
			baseIcon.shadow = '';
			baseIcon.iconSize = new GSize(22, 22);
			baseIcon.iconAnchor = new GPoint(11, 11);
			baseIcon.infoWindowAnchor = new GPoint(11, 11);
			baseIcon2 = new GIcon(G_DEFAULT_ICON);
			baseIcon2.shadow = '';
			baseIcon2.iconSize = new GSize(22, 22);
			baseIcon2.iconAnchor = new GPoint(10, 35);
            
            baseIcon4 = new GIcon(G_DEFAULT_ICON);
            baseIcon4.shadow = '';
            baseIcon4.iconSize = new GSize(32, 32);
            baseIcon4.iconAnchor = new GPoint(16, 16);		
			//============================= chuyen thanh ban do tieng viet ==============================
			
            obj.map.addControl(new GMapTypeControl());
            obj.map.enableScrollWheelZoom();
            
            // add custommap and orverlay wikimapia start
            // Set up the copyright information
            var myCopyright = new GCopyrightCollection("© ");
            myCopyright.addCopyright(new GCopyright('Demo',new GLatLngBounds(new GLatLng(-90,-180), 
                        new GLatLng(90,180)),
                        0,'©2007 Google'));
            //********************************
            // VBD on google
           
           var GMapTypeOptions = new Object();
            GMapTypeOptions.minResolution = 0;
            GMapTypeOptions.maxResolution = 20;
            GMapTypeOptions.errorMessage = "No map data available";
           //var maptype_VBD1 = new GMapType(Only_VBD1, new GMercatorProjection(22),vietBD,GMapTypeOptions);
            //**********************************
            // Create the tile layer overlay and             
            var wiki_tilelayer = new GTileLayer(myCopyright);
            wiki_tilelayer.getTileUrl = obj.GetTileUrlWikimapia;
            wiki_tilelayer.isPng = function() { return true;};
            wiki_tilelayer.getOpacity = function() { return 1.0; }

                
            if(type == 0)
            {
                var crossLayer = new GTileLayer(new GCopyrightCollection(""), 0, 15);
                crossLayer.getTileUrl =  function(tile, zoom) {
                return "./include/tile_crosshairs.png";
                }
                crossLayer.isPng = function() 
	            {
		            return true;
	            }
                var tilelayers1 = [G_NORMAL_MAP.getTileLayers()[0],crossLayer];
                var tilelayers2 = [G_HYBRID_MAP.getTileLayers()[0],crossLayer,G_HYBRID_MAP.getTileLayers()[1]];
                var tilelayers3 = [G_SATELLITE_MAP.getTileLayers()[0],crossLayer];
                var tilelayers4 = [G_PHYSICAL_MAP.getTileLayers()[0],crossLayer];
                var Satelllite_Wiki = [G_SATELLITE_MAP.getTileLayers()[0],wiki_tilelayer];
                // var tilelayers5 = [G_WIKI_MAP.getTileLayers()[0],crossLayer];
                // TRB003 Start edit
                ///////////////////////////////
                // Phải cho phép zoom đến mức 22
                var NORMAL = new GMapType(tilelayers1, new GMercatorProjection(22),map1);
                var SATELLITE = new GMapType(tilelayers3, new GMercatorProjection(22),map2);  
                var HYBRID = new GMapType(tilelayers2, new GMercatorProjection(22),map3);
                var PHYSICAL = new GMapType(tilelayers4, new GMercatorProjection(22),map4);
                var WIKI = new GMapType(Satelllite_Wiki, new GMercatorProjection(22),map5);
                // TRB003 End Edit
                var mapdetail = $("#hd_mapdetail").val();
                obj.map.addMapType(NORMAL);
                obj.map.addMapType(SATELLITE);
                obj.map.addMapType(HYBRID);
                if (mapdetail == 15)
                { 
                obj.map.addMapType(PHYSICAL);
                }
                //obj.map.addMapType(maptype_VBD1); 
                obj.map.addMapType(WIKI); 

                obj.map.setCenter(new GLatLng(latinit, lnginit), 15);
            }
            else
            {
                // Create the copyright collection
                var copyCollection = new GCopyrightCollection('RasterMap');
                var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(21.0229825, 105.8422852), new GLatLng(21.0332372, 105.8587646)), 0, "Created by Global Mapper");
                copyCollection.addCopyright(copyright);
                
                // Setup the tile layers
                var tilelayers = new Array();
                //tilelayers[0] = G_SATELLITE_MAP.getTileLayers()[0];
                tilelayers[0] = new GTileLayer(copyCollection, 0, 20 );
                tilelayers[0].getTileUrl = CustomGetTileUrl
                tilelayers[0].isPng = function() {return true;};
                
                // Setup the options for our map layer
                var GMapTypeOptions = new Object();
                GMapTypeOptions.minResolution = 0;
                GMapTypeOptions.maxResolution = 20;
                GMapTypeOptions.errorMessage = "No map data available";
			    
			    // Create our custom map layer
                var custommap = new GMapType(tilelayers, new GMercatorProjection(22), "ada", GMapTypeOptions);
                custommap.getTextColor = function() {return "#000000";};
                obj.map.addMapType(custommap);
                obj.map.setCenter(new GLatLng(latinit, lnginit), 13, custommap);
            }	
			obj.map.removeMapType(G_NORMAL_MAP);
            obj.map.removeMapType(G_HYBRID_MAP);
            obj.map.removeMapType(G_SATELLITE_MAP);
            obj.map.removeMapType(G_PHYSICAL_MAP);
           // obj.map.removeMapType(G_WIKI_MAP);
			//=====================================================================================
			obj.loaded=true;    
            // start trung add
			Gmap = obj.map;
            Gwaypoints =new Array();
            Gmarker = null;
            Gobjpolyline = new Array() ;
            // end trung add
            

             GEvent.addListener(Gmap, 'infowindowclose', function()
                {
                   if (_global.IsPlaying == false)
                   {
                    document.getElementById('hdMoveEnd').value = 0;
                   }
                   else
                   {
                    document.getElementById('hdMoveEnd').value = 1;
                   }
                });     
            // end phucnh
            //Phuong add bắt sự kiên wiki changed
            //start lấy sự kiên changemap
            GEvent.addListener(Gmap, 'maptypechanged', function()
                {              
                    var maptype = obj.map.getCurrentMapType();
                    //alert(maptype);
                    obj.currentMapName = maptype.getName(false);
                    if(obj.currentMapName == map5)
                    {                            
                        obj.ShowWiki();
                        if( obj.wikiMoveEnd==null)
                        {
                            obj.wikiMoveEnd = GEvent.addListener(obj.map, 'moveend', function()
                            {                          
                                obj.ShowWiki();
                            });
                        }
                        if(obj.wikiZoomEnd==null)
                        {
                            obj.wikiZoomEnd = GEvent.addListener(obj.map, 'zoomend', function(oldl, newl)
                            {               
                                obj.ShowWiki();
                            });     
                        }                   
                    }
                    else
                    {                       
                        if(obj.wikiMoveEnd != null)
                        {
                            GEvent.removeListener(obj.wikiMoveEnd);     
                            obj.wikiMoveEnd=null;                                                   
                        }
                        if(obj.wikiZoomEnd != null)
                        {
                            GEvent.removeListener(obj.wikiZoomEnd);
                            obj.wikiZoomEnd=null;
                        }
                        obj.StopWiki();
                    }   
                });
            //end lấy sự kiện changemap
            //end Phuong add sự kiên wiki changed
			GEvent.addListener(Gmap, "click", function(overlay,point)
			 {	             
				if(typeof(point)!="undefined")
				{  
                    if($('#hdDrawMarker').val()==1){
                        
                    if(Gmarker!=null){
                       obj.map.removeOverlay(Gmarker); 
                       Gmarker=null; 
                     }                       
                    if(Gmarker==null)
                        Gmarker = new GMarker(point);               
                        obj.map.addOverlay(Gmarker); 
                       $('#curLat').val(point.lat());  
                       $('#curLong').val(point.lng()); 
                       $('#hdlat').val(point.lat());  
                       $('#hdlong').val(point.lng());                        
                    }
                     
                }	
			 });
			 
	    }
	    else
		{
			alert(sorryg);
	    }
        
        // Add-Start by Lamdhn: YC101 - 2011.04.13
        obj.PolyDragControl = new MPolyDragControl({map:obj.map, type:'rectangle'});
        obj.PolyDragControl.disableTransMarker();
        // Add-End by Lamdhn: YC101 - 2011.04.13 
            


	}

	obj.ZoomTo = function(nLat, nLong, nLevel)
	{
		obj.map.setCenter(new GLatLng(nLat, nLong),nLevel);
	}
    obj.MoveEnd = function(nLat, nLong)
    {
        var g_LatLngBound = obj.map.getBounds();
        if (g_LatLngBound.containsLatLng(new GLatLng(nLat, nLong)) == false)
        {
           obj.map.setCenter(new GLatLng(nLat, nLong));    
        }
    }
	
	obj.PanTo = function(nLat, nLong)
	{
		//obj.map.panTo(new GLatLng(nLat, nLong));
		obj.map.setCenter(new GLatLng(nLat, nLong));
	}
    
    obj.AddOnClick = function()
    {    
        var objBind = GEvent.addListener(obj.map, "click", function(overlay, pt)
        {
            if(typeof(pt)!='undefined')
            {    
                var rec=obj.CreateRec(pt.lat(),pt.lng(),1);
                
                var strHTML = '<div class = "info" style="width:280px; height:225px;z-index:0; margin-top:0px;" >'+new_loc+
                                '<from id ="VM"  style="margin-top:0px;" >'+
                                    '<table style="margin-top:0px;" border=0 cellpadding=1 cellspacing=1>'+
                                        '<tr>'+
                                            '<td width="100px" align="right">'+new_name+'</td>'+
                                            '<td width="150px"><input type="text" name="txtLocName" id="txtLocName" value="" maxlength="60" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_address+'</td>'+
                                            '<td><input type="text" name="txtAddress" id="txtAddress" value="" maxlength="200" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_loc_type+'</td>'+
                                            '<td>'+
                                            '<select name="ddlLocType" id="ddlLocType" style="width:152px;">'+
                                            '<option value="">'+new_loc_type_select+'</option>';
                                            for (var i=0; i<_arrLocationType.length; i++) {
                                                strHTML += '<option value="'+_arrLocationTypeId[i]+'">'+_arrLocationType[i]+'</option>';
                                            }
                                strHTML += '</select>'+
                                            '</td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_contact+'</td>'+
                                            '<td><input type="text" name="txtContact" id="txtContact" value="" maxlength="200" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_phone+'</td>'+
                                            '<td><input type="text" name="txtPhone" id="txtPhone" value="" maxlength="20" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_amount+'</td>'+
                                            '<td><input type="text" size="2" name="txtFee" id="txtFee" value="" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_radius+'</td>'+
                                            '<td><input type="text" size="2" name="txtRad" id="txtRad" value="1" style="width: 40px;" maxlength="2"> '+units+' 100m</td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+description+'</td>'+
                                            '<td><textarea name="txtDesc" id="txtDesc" maxlength="225" style="width: 150px; height: 40px; border: solid 1px green; overflow: auto;"></textarea></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td></td>'+
                                            '<td align="left">'+
                                                '<input type="submit" onclick="_global.locs.SaveLoc()" value="'+btnsave+'" name="B1">'+
                                                '<input type="reset" value="'+btncancel+'" name="B2" onclick="_global.myMap.HideInfo();_global.myMap.DeleteLocation(_global.myMap.locCreating);">'+
                                            '</td>'+
                                        '</tr>'+
                                    '</table>'+
                                '<input type="hidden" name="clickLat"  id="clickLat"  value='+pt.lat()+'>'+
                                '<input type="hidden" name="clickLng"  id="clickLng"  value='+pt.lng()+'>'+
                                '</from>'+
                              '</div>';
                              
                obj.locCreating=obj.DrawLocationRec(rec.lat1,rec.lng1,rec.lat2,rec.lng2);
                obj.map.openInfoWindow(pt,strHTML);

                var info = obj.map.getInfoWindow();
                GEvent.addListener(info, 'closeclick', function(){
                    _global.myMap.HideInfo();
                    _global.myMap.DeleteLocation(_global.myMap.locCreating);
                });
            }            
        }                
        );        
        return(objBind);
    }
    
	obj.RemoveOnClick = function(myEventListener)
	{
		GEvent.removeListener(myEventListener);	
	}
	obj.Point=function(nLat,nLong)
	{
		return (new GLatLng(nLat,nLong));
	}
	obj.CheckHide=function(icon)
	{
		if(icon.isHidden()==false)
			return false;
		else 
			return true;
	}
	obj.GetLevel=function()
    {
        return obj.map.getZoom();
    }
    obj.GetCenter=function() //obj.latitude, longitude
    {
        var result=new Object();
        var p= obj.map.getCenter(); 
        result.latitude=p.lat();
        result.longitude= p.lng(); 
        return result;
    }	
	obj.ClearOverlay=function()
	{
		obj.map.clearOverlays();
	//	alert("clear");
	}
	obj.CreateLocal = function(latclick,lngclick)
	{
		var radius = 4*1*1.609344;
		var latConv = 7347.086392356055;
		var	lngConv = 7215.3469046674845;
		var point1 = new GLatLng(latclick + (radius/latConv * Math.cos(0* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(0 *Math.PI/180)));								
		var point2 = new GLatLng(latclick + (radius/latConv * Math.cos(90* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(90 *Math.PI/180)));
		var point3 = new GLatLng(latclick + (radius/latConv * Math.cos(180* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(180 *Math.PI/180)));								
		var point4 = new GLatLng(latclick + (radius/latConv * Math.cos(270* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(270 *Math.PI/180)));												
		
		var p1 = new GLatLng(point1.lat(),point4.lng());
		var p2 = new GLatLng(point1.lat(),point2.lng());
		var p3 = new GLatLng(point3.lat(),point2.lng());
		var p4 = new GLatLng(point3.lat(),point4.lng());
		
		obj.PanTo(latclick,lngclick);
		var arrLatLngs = new Array(p1, p2, p3, p4, p1);
		var polygon = new GPolygon(arrLatLngs, "#ff0000",1,1,"#CCFFDF",0.2,{clickable:false});
		obj.map.addOverlay(polygon);
	}
	obj.CreateRec = function(lat,lng,rad)
	{
		if(_global.myMap.locCreating)
			_global.myMap.DeleteLocation(_global.myMap.locCreating);
			
		var latclick = parseFloat(lat);
		var lngclick = parseFloat(lng);

		var result=new Object();
		var radius = 4*rad*1.609344;
		var latConv = 7347.086392356055;
		var	lngConv = 7215.3469046674845;
		var point1 = new GLatLng(latclick + (radius/latConv * Math.cos(0* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(0 *Math.PI/180)));								
		var point2 = new GLatLng(latclick + (radius/latConv * Math.cos(90* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(90 *Math.PI/180)));
		var point3 = new GLatLng(latclick + (radius/latConv * Math.cos(180* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(180 *Math.PI/180)));								
		var point4 = new GLatLng(latclick + (radius/latConv * Math.cos(270* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(270 *Math.PI/180)));	
		
		result.lat1=point1.lat();
		result.lng1=point4.lng();
		result.lat2=point3.lat();
		result.lng2=point2.lng();
		return result;
	}
	obj.Contains = function(polygon,point)
	{
        var j=0;
        var oddNodes = false;
        var x = point.lng();
        var y = point.lat();
		if(typeof(polygon)!='undefined')
		{	
			for (var i=0; i < polygon.getVertexCount(); i++) {
			  j++;
			  if (j == polygon.getVertexCount()) {j = 0;}
			  if (((polygon.getVertex(i).lat() < y) && (polygon.getVertex(j).lat() >= y))
			  || ((polygon.getVertex(j).lat() < y) && (polygon.getVertex(i).lat() >= y))) {
				if ( polygon.getVertex(i).lng() + (y - polygon.getVertex(i).lat())
				/  (polygon.getVertex(j).lat()-polygon.getVertex(i).lat())
				*  (polygon.getVertex(j).lng() - polygon.getVertex(i).lng())<x ) {
				  oddNodes = !oddNodes
				}
			  }
			}
		}	
        return oddNodes;
      }
	  obj.Makeinfo = function(index)
	{	
		icn = new GIcon(baseIcon);
		icn.image='images/spacer.gif';
       
		var info = index.name;
		info=ToName(info);
		var latlng = new GLatLng(index.lat1,index.lng1); 	
		var opts = { 
		          icon: icn,
		          //clickable: true,
		          title:info,
				  labelText: "<font size ='2' color='blue'> "+info+"</font>",
		          labelOffset: new GSize(0,-15)
				};
		var marker = new LabeledMarker(latlng,opts);
		obj.map.addOverlay(marker);
        
		return marker;			
	}
	function ToName(name)
	{
		try
		{	
			var arr1 = new Array();
			var str='';
			arr1 = name.split(" ");
			for(i=0;i<arr1.length-1;i++)
				str+=arr1[i]+"_";
			str+=arr1[arr1.length-1];
			return str;
		}
		catch(e)
		{
			return name;
		}
	}
	obj.CreateIconMap=function(newObj)
	{
        
		var name   	  = newObj.name;
        
		var lats1	  = newObj.lat1;
		var lngs1     = newObj.lng1;
		var lats2	  = newObj.lat2;
		var lngs2     = newObj.lng2;		
		var links     = newObj.link;
		
		var lat = (lats1 + lats2)/2;
		var lng = (lngs1 + lngs2)/2;
		
		
		var pnt = new GLatLng(lat, lng);

		icn = new GIcon(baseIcon);
		icn.image=links;	
		
		var tilte = name;
		
		tilte=tilte.replace(" ","_");
        
        var textname ='';          
        if(obj.GetLevel()>=15){
           textname =  "<font color='blue'>"+tilte+"</font>";
        }       
        
		var opts = 
			{ 
		      icon: icn,
		      clickable: true,
		      title: tilte,
		      labelText: textname,
		      labelOffset: new GSize(-20,0)
			};
            
		var marker2 = new LabeledMarker(pnt,opts);
		obj.map.addOverlay(marker2);
        
		return marker2;		
	}
	obj.DrawPolyline=function(latnew,longnew,latold,longold)
	{
		
		 var polyline = new GPolyline([
			new GLatLng(latnew,longnew),
			new GLatLng(latold,longold)
			], "#ff0000", 3);
		obj.map.addOverlay(polyline);
		return polyline;
	}
    //****************************************************************************************************************
    // region wiki     
    obj.aname = new Array();
    obj.arrIdWikiObj = new Array();
    obj.arrPolygonWiki = new Array();
    obj.posWiki = 0;
    obj.GetTileUrlWikimapia=function (a,b)
    { 
      var i=a.x%4;
      var j=a.y%4;
      var temp=j*4+i;  
      return "http://i"+temp+".wikimapia.org/?x="+a.x+"&y="+a.y+"&zoom="+b+"&type=hybrid&lng=38";
    }
    obj.StopWiki=function()
    {
        //$("#divwiki").css("display",'none'); 
        var i = 0;
         for(i = 0; i < obj.arrPolygonWiki.length; i++)
         {
             GEvent.clearListeners(obj.arrPolygonWiki[i],"mousemove");
             GEvent.clearListeners(obj.arrPolygonWiki[i],"mouseout");
             Gmap.removeOverlay(obj.arrPolygonWiki[i]);
             //Gmap.clearOverlays();
         }
              
         $(".tooltipmap").css("visibility","hidden");     
         obj.aname = new Array();
         obj.arrPolygonWiki = new Array();   
         obj.arrIdWikiObj = new Array();     
         obj.posWiki = 0;
         //$("#divwiki").css("display",'');
             
    }
    //Cross domain
    obj.CreateCrossRequest=function(method, url)
    {
        var xhr = new XMLHttpRequest();
        //alert("xmlrequest");
        if ("withCredentials" in xhr){
            xhr.open(method, url, true);
        } else if (typeof XDomainRequest != "undefined"){
            //alert("xdomain");
            xhr = new XDomainRequest();
            xhr.open(method, url);
        } else {
            xhr = null;
        }
        return xhr;
    }
    obj.ShowWiki=function()
    {
        //$("#divwiki").css("display",'none');
        var bounds = Gmap.getBounds();
        var sw = bounds.getSouthWest();
        var ne = bounds.getNorthEast();   
        //alert("lon_min=" + sw.lng() + "&lon_max=" + ne.lng() + "&lat_min=" + sw.lat() + "&lat_max=" + ne.lat());
        var count = 500;
        var page = 1; 
        var xmlhttp = null;        
        var st = "http://api.wikimapia.org/?function=box&key=3AAAD569-8E95440A-1EB7F4A0-67294FA7-48F26AB9-F3212F16-E593AF24-41A1508&lon_min=" + sw.lng() + "&lon_max=" + ne.lng() + "&lat_min=" + sw.lat() + "&lat_max=" + ne.lat() + "&format=json&page=" + page + "&count=" + count;
        var request = obj.CreateCrossRequest("get",st);
        if (request)
        {
            request.onload = function()
            {
                //alert(request.responseText);  
                 if(obj.currentMapName != map5)
                 {
                     return;
                 }                     
                var json = request.responseText;
                if(typeof(json) != 'object')
                {  
                    json = $.parseJSON(json);
                }               
                json = json.folder;          
                var i = 0;
                var j = 0;
                for(i = 0; i < json.length; i++)
                {// FakeWiki
                    // Kiem tra ton tai                       
                    if(obj.arrIdWikiObj.indexOf(json[i].id) == -1)
                    {
                        obj.arrIdWikiObj[obj.posWiki] = json[i].id;
                        obj.aname[obj.posWiki] = json[i].name;
                        // Get location
                        var poly_points = json[i].polygon;
                        var points = new Array();
                        for(j = 0; j < poly_points.length; j++)
                        {
                            points[j] = new GLatLng(poly_points[j].y, poly_points[j].x);
                        }
                        obj.arrPolygonWiki[obj.posWiki] = new GPolygon(points, '#FFFFFF', 1, 1, '#FF0000', 0.0);
                        Gmap.addOverlay(obj.arrPolygonWiki[obj.posWiki]);               
                        obj.AddEvents_Wiki(obj.posWiki);
                        obj.posWiki++;
                    }
                }
            };
            request.send();
        }
    }
    obj.AddEvents_Wiki=function(i)
    {     
        var mousemovewiki = GEvent.addListener(obj.arrPolygonWiki[i], 'mousemove', function(){ 
            var opts = {color: '#fecd0e', weight: 1, opacity: 0.2};
            obj.arrPolygonWiki[i].setFillStyle(opts);
            opts = {color: '#fecd0e', weight: 1, opacity: 1};
            obj.arrPolygonWiki[i].setStrokeStyle(opts);
            $(".tooltipmap").css("visibility","hidden");
            sTooltip(obj.arrPolygonWiki[i],obj.aname[i]);       
        });
        var mouseoutwiki = GEvent.addListener(obj.arrPolygonWiki[i], 'mouseout', function(){
            var opts = {color: '#FF0000', weight: 1, opacity: 0.0};
            obj.arrPolygonWiki[i].setFillStyle(opts);
            opts = {color: '#FFFFFF', weight: 1, opacity: 1};
            obj.arrPolygonWiki[i].setStrokeStyle(opts);        
            $(".tooltipmap").css("visibility","hidden");               
        });
    }
    
    // end region wiki
    //********************************
	return obj;    
}

//phuong add wiki




function sTooltip(polygon,aname,t)
{          
    
    var mousex = document.getElementById("toadoX").value;//1020
    var mousey = document.getElementById("toadoY").value;
    var onresize = document.getElementById("mapdivonresize").value;
    if(onresize == 0)
    {          
        mousex-=355;//650
        mousey-=35;
       // alert(mousey);
        if(mousex >= 550)  mousex-=100;
        if(mousey >= 550) mousey-=40;
        if(mousey <= 0) mousey+=40;
    }           
    var mapdiv = document.getElementById("maptracking");
    var tooltip = document.createElement("div");
    tooltip.id="tooltipmap";
    mapdiv.appendChild(tooltip);        
    polygon.tooltip = '<div class="tooltipmap"><table><tr><td>'+aname+'</td></tr></table></div>';
    tooltip.innerHTML = polygon.tooltip ;
    $(".tooltipmap").css("left",mousex.toString()+"px");
    $(".tooltipmap").css("top",mousey.toString()+"px");               
    tooltip.style.visibility ='visible';       
}


//end phuong add 
function CustomGetTileUrl(a,b)
{//alert("http://map2.vngeobiz.com/tilemap/tilemapservice?lvl=" + b + "&top="+ a.y + "&left=" + a.x + "&type=vector");
    return "http://map2.vngeobiz.com/tilemap/tilemapservice?lvl=" + b + "&top="+ a.y + "&left=" + a.x + "&type=vector";
    //return "file:///C:/Documents and Settings/T61P/Desktop/Google/Google/Z" + b + "/" + a.y + "_" + a.x + ".png"; // replace that with a "real" URL
}
